package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.exception.CommentAttentException;
import com.woniuxy.commons.exception.code.CommentAttentExceptionCode;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.CommentAttentCreateForm;
import com.woniuxy.dao.model.CommentAttachment;
import com.woniuxy.dao.CommentAttachmentDao;
import com.woniuxy.service.CommentAttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class CommentAttachmentServiceImpl extends ServiceImpl<CommentAttachmentDao, CommentAttachment> implements CommentAttachmentService {
    @Resource
    private CommentAttachmentService commentAttachmentService;
    @Override
    public boolean addCommentAttach(CommentAttentCreateForm commentAttent) {
        CommentAttachment commentAttachment = BeanCopyUtil.copyOne(commentAttent, CommentAttachment.class);
        boolean save = commentAttachmentService.save(commentAttachment);
        if(save){
            return true;
        }else{
            throw new CommentAttentException(CommentAttentExceptionCode.COMMENT_ADD_FAIL.getCode(),CommentAttentExceptionCode.COMMENT_ADD_FAIL.getMessage());
        }

    }

    @Override
    public ArrayList<String> getCommentAttent(Long commentId) {
        List<CommentAttachment> commentAttachments = commentAttachmentService.list(new QueryWrapper<CommentAttachment>().eq("commentId", commentId));
        if(null==commentAttachments){
            return null;
        }else{
            ArrayList<String> commentAttachment=new ArrayList<>();
            for (CommentAttachment attachment : commentAttachments) {
                commentAttachment.add(attachment.getFilePath());
            }
            return commentAttachment;
        }
    }
}
